$(document).ready(function(){
    $("#login_btn").click(function (){
        const userAccount = $.trim($("#login input[title='userAccount']").val());
        const userPassword = $.trim($("#login input[title='userPassword']").val());

        if (userAccount === "") {
            $("#login input[title='userAccount']").attr("placeholder","用户名不能为空");
        }
        if (userPassword === "") {
            $("#login input[title='userPassword']").attr("placeholder","密码不能为空");
        }else {
            $.ajax({
                type: "post",
                url: "/api/user/v/login",
                dataType: "text",
                data: $("#login").serialize(),
                success: data => {
                    data = eval('('+ data +')');
                    if (data.success) {
                        $.cookie("userAccount", userAccount);
                        location.href = '/index';
                    }else {
                        alert("账户名或者密码错误!");
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    const msg = xhr.responseText;
                    console.log(msg);
                }
            });
        }
    })

    $("#register_btn").click(function () {
        const userAccount = $.trim($("#register input[name='userAccount']").val());
        const userName = $.trim($("#register input[name='userName']").val());
        const userPassword = $.trim($("#register input[name='userPassword']").val());
        const userComfirmPassword = $.trim($("#register input[name='userComfirmPassword']").val());
        const userSex = "";
        const userAge = 0;

        if (userAccount === "") {
            $("#login input[title='userAccount']").attr("placeholder","用户名不能为空");
        }
        if (userPassword === "") {
            $("#login input[title='userPassword']").attr("placeholder","密码不能为空");
        }
        if (userPassword !== userComfirmPassword) {
            $("#login input[title='userPassword']").attr("placeholder","两次密码不一致");
        }else {
            $.ajax({
                type: "post",
                url: "/api/user/v/register",
                dataType: "text",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify({
                    "userAccount": userAccount,
                    "userPassword": userPassword,
                    "userName": userName,
                    "userSex": userSex,
                    "userAge": userAge
                }),
                success: data => {
                    data = eval('('+ data +')');
                    if (data.success) {
                        $.cookie("userAccount", userAccount);
                        alert("注册成功!");
                    }else {
                        alert(data.msg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    const msg = xhr.responseText;
                    console.log(msg);
                }
            });
        }
    })
});


function addRecommend() {
    let result;
    $.ajax({
        type: "get",
        url: "/api/recommendBook/u/addRecommendBooks",
        dataType: "text",
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                result = JSON.parse(data.data);
            }else {
                result = [];
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}